# -*- encoding: utf-8 -*-

# Copyright 2010 István Szentandrási
# <etude-music-player@googlegroups.com>

# This file is part of Etude.
#
# Etude is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as 
# published by the Free Software Foundation, either version 3 
# of the License, or (at your option) any later version.
#
# Etude is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Etude. If not, see <http://www.gnu.org/licenses/>.

from common import *

class Config(dict):
    """
    wrapper around the xmms_config* functions
    lot of stuff changed in git configval -> config_value etc
    """

    def __init__(self, client):
        dict.__init__(self)
        self.client = client
        self.client.connect("e_configval_changed", self.__changed)

    @check_fast(False)
    @check_wait(False)
    def load(self):
        """load configs"""
        self.clear()
        try:
            res = self.client.xmms.config_list_values(self.__config_cb)
        except AttributeError: #xmms2 <= 0.6
            res = self.client.xmms.configval_list(self.__config_cb)
        return res

    @check_result_error()
    def __config_cb(self, result):
        for key, value in result.value().items():
            self[key] = value
            self.client.emit("e_configval_changed", key, value)
        return False
    
    def __changed(self, widget, key, value):
        self[key] = unicode(value)
        return False

    @check_fast(False)
    @check_wait(False)
    def get(self, key, cb = None):
        """
        get a config value from the server
        usually it's enough to use: 
            conf = client.config
            config_value = conf[key]
        As it should be updated automatically
        """
        if cb == None:
            cb = self.__config_cb
        try:
            res = self.client.xmms.config_get_value(unicode(key), cb)
        except AttributeError: # xmms2 <= 0.6
            res = self.client.xmms.configval_get(unicode(key), cb)
        return res
    

    @check_fast(False)
    @check_wait(False)
    def register(self, key, default_value,cb = None):
        """
        register a key. xmms2 won't allow setting a configuration value for a nonexisting
        key before registering
        """
        if cb == None:
            cb = self.__config_cb
        try:
            res = self.client.xmms.configval_register(unicode(key), unicode(default_value), cb)
        except AttributeError:
            res = self.client.xmms.config_register_value(unicode(key), unicode(default_value),cb)
        return res

    @check_cb_None()
    @check_wait(False)
    @check_fast(False)
    def set(self, key, value, cb = None):
        """
        set a configuration value
        """
        try:
            res = self.client.xmms.config_set_value(unicode(key), unicode(value), cb)
        except AttributeError: #xmms2 <= 0.6
            res = self.client.xmms.configval_set(unicode(key), unicode(value), cb) 
        return res

    def set_effect(self, effect):
        """
        function to make xmms2 effect activation easier
        """
        effects = [key for key in self.keys() if key.startswith('effect.order.')]
        ind = ''
        for key in effects:
            if self[key] == effect:
                return True
            elif len(self[key]) == 0:
                if ind == '':
                    ind = key
                else:
                    ind = min(key, ind)
        if ind != '':
            self.set(ind, effect, fast=True)
            return True
        return False

    def unset_effect(self, effect):
        """
        deactivate an effect
        """
        keys = [key for key in self.keys() if (key.startswith('effect.order.')) and (self[key] == effect)]
        for key in keys:
            self.set(key, u'', fast=True)
            
